﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Southridge.View
{
    public enum Views
    {
        Search, 
        Details, 
        Profile,
        Map 
    }; 

    public  class ViewManager
    {
        private FrameworkElement _current;
        private Panel _root;
        private FrameworkElement _oldView ;
        private Dictionary<Views, FrameworkElement> _activeViews = new Dictionary<Views,FrameworkElement>();
        private bool _allowsBackView;


        private ViewManager(Panel root, bool allowsBack)
        {
            if (root == null)
                throw new ApplicationException(Resources.ViewManagerNeedsRoot); 
            _root = root;
            _allowsBackView = allowsBack; 
        } 

        public bool AllowsBackView
        {
            get
            {
                return _allowsBackView; 
            }
            set
            {
                _allowsBackView = true; 
            }
        } 

        public FrameworkElement BackView
        {
            get
            {
                return _oldView; 
            }
        }
 
        public FrameworkElement GetView( Views view )
        {
            FrameworkElement fe = null; 
            bool found = _activeViews.TryGetValue(view, out fe);  ;
            
            if ( !found || fe == null)
            {
                switch (view)
                {
                    case Views.Profile:
                        fe = new NewProfile(); 
                        break;  
                    case Views.Map :
                        fe = new MapView();
                        break; 
                    default: 
                        System.Diagnostics.Debug.Assert (false, "not implemented yet"); 
                        break; 
                }
                if (fe != null)
                    _activeViews.Add(view, fe); 
            }
            return fe; 
        } 

        private static ViewManager _currentViewManager; 
        public static ViewManager Create ( Panel  root , bool allowsBack ) 
        {
            if (ViewManager._currentViewManager != null) 
            { 
                throw new ApplicationException ( Resources.OnlyOneViewManagerSupported); 
            } 

            ViewManager vm = new ViewManager ( root, allowsBack ) ;
            ViewManager._currentViewManager = vm;
            return ViewManager._currentViewManager; 

        } 

        public static ViewManager Current 
        { 
            get
            { 
                return _currentViewManager; 
            } 
        } 
            
        public void Add (  FrameworkElement child, Views viewtype )
        {
            _activeViews.Add (  viewtype, child);
            if (_current == null)
                _current = child; 
        }

        public void Remove( Views viewtype )
        {
            _activeViews.Remove(viewtype); 
        } 

        public  FrameworkElement CurrentView
        {
            get
            {
                return _current; 
            }
        }

        public  void Transition( FrameworkElement newView )
        {
            if (newView == CurrentView || newView == null )
                return; 


            newView.SetValue(Grid.MarginProperty, new Thickness(_root.ActualWidth, 0, 0, 0)); 
            if (!_root.Children.Contains(newView))
            {
                // newView.SetValue(Canvas.LeftProperty, _root.ActualWidth);
                // newView.SetValue(Canvas.TopProperty, 0.0);               
                _root.Children.Add(newView);
            }
            else
            {
                newView.Visibility = Visibility.Visible; 
            }

            _oldView = CurrentView;
            _current = newView; 
            Storyboard sb = new Storyboard(); 
            Duration duration = new Duration( TimeSpan.FromMilliseconds (200)); 
            ThicknessAnimation da  = new ThicknessAnimation (  new Thickness ( 0,0,0,0) , duration );
            da.Completed += new EventHandler(da_Completed);
            

            newView.BeginAnimation( FrameworkElement.MarginProperty , da); 
        }

      

        void da_Completed(object sender, EventArgs e)
        {
            if (_oldView != null)
                _oldView.Visibility = Visibility.Collapsed;

        }

        public bool? ShowWindow(Window w, bool modal)
        {
            if (modal)
            {
                Storyboard sb = Application.Current.MainWindow.Resources["ModalOn"] as Storyboard ;
                if (sb != null)
                {
                    sb.Begin(); 
                } 
                bool? ret = w.ShowDialog();
                Storyboard sbOff = Application.Current.MainWindow.Resources["ModalOff"] as Storyboard;
                if (sbOff != null)
                {
                    sbOff.Begin(); 
                } 
                _current.Opacity = 1.0; 
            } 
            else
                w.Show();

            return true; 
        } 
    }
}
